﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FbKPI.Model;
using FbKPI.Facade;
using System.Web.Script.Serialization;
using System.Collections.ObjectModel;
using System.Web.Script.Services;

namespace FBKPI.Serializer
{
    /// <summary>
    /// Summary description for KPIAssignDetailNonSerializer
    /// </summary>
    public class KPIAssignDetailNonSerializer : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            string result = this.GetAllData();
            context.Response.Write(result);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string[] Columns
        {
            get
            {
                return new string[]
                {
                   "ID",
                   "No",
                   "Description",
                   "isActive",
                   "AssignedEmployee",
                   //"KPIParent",
                   "GroupKPI",
                   "CategoryKPI",
                   "TypeKPI",
                   "SubTypeKPI",
                   "Periode",
                   "Weight"
                    
			       
                };
            }
        }

        private string GetAllData()
        {
            int sEcho = Convert.ToInt16(HttpContext.Current.Request.Params["sEcho"]);
            int iDisplayLength = Convert.ToInt16(HttpContext.Current.Request.Params["iDisplayLength"]);
            int iDisplayStart = Convert.ToInt16(HttpContext.Current.Request.Params["iDisplayStart"]);
            int iSortCol_0 = Convert.ToInt16(HttpContext.Current.Request.Params["iSortCol_0"]);
            string sSortDir = HttpContext.Current.Request.Params["sSortDir_0"];
            string sSearch = HttpContext.Current.Request.Params["sSearch"];
            string sSortColumn = Columns[iSortCol_0];
            int UserID = Convert.ToInt16(HttpContext.Current.Request.Params["User"]);
            int year = HttpContext.Current.Request.Params["Year"].Replace("undefined", string.Empty) == string.Empty ? DateTime.Now.Year : Convert.ToInt16(HttpContext.Current.Request.Params["Year"]);
            bool Mandatory = Convert.ToInt16(HttpContext.Current.Request.Params["Mandatory"]) == 1 ? true : false;
            IList<KPI> result = new List<KPI>();
            KPIFacade pf = new KPIFacade();
            int iTotalRecords = 0;

            //if(Mandatory==false)
                result = pf.GetAllKPIToBeAssignedNonMandatory(sSearch, iDisplayStart, iDisplayLength + iDisplayStart, sSortColumn, sSortDir, out iTotalRecords, new Employee() { ID = UserID }, year);
            //else
            //result = pf.GetAllKPIToBeAssigned(sSearch, iDisplayStart, iDisplayLength + iDisplayStart, sSortColumn, sSortDir, out iTotalRecords, new Employee() { ID = UserID }, year, Mandatory);
            int iTotalDisplayRecords = 0;

            //iTotalDisplayRecords = iTotalRecords;

            iTotalRecords = Convert.ToInt32(pf.GetAllKPIToBeAssignedCountNon(sSearch, new Employee() { ID = UserID }, year));
            iTotalDisplayRecords = iTotalRecords;

            IDictionary<int, IDictionary<string, string>> collection = new Dictionary<int, IDictionary<string, string>>();

            if (result.Count > 0)
            {
                int idx = iDisplayStart;
                foreach (KPI p in result)
                {
                    idx++;
                    IDictionary<string, string> ColumnValues = new Dictionary<string, string>();
                    ColumnValues.Add(this.Columns[0], p.ID.ToString());
                    ColumnValues.Add(this.Columns[1], idx.ToString());
                    ColumnValues.Add(this.Columns[2], p.Description.ToString());
                    ColumnValues.Add(this.Columns[3], p.isActive.ToString());
                    ColumnValues.Add(this.Columns[4], RetrieveAssignedEmployee(p.ID, UserID));
                    //ColumnValues.Add(this.Columns[5], p.KPIParent == null ? string.Empty : p.KPIParent.Description.ToString());
                    ColumnValues.Add(this.Columns[5], p.GroupKPI == null ? string.Empty : p.GroupKPI.Description.ToString());
                    ColumnValues.Add(this.Columns[6], p.CategoryKPI == null ? string.Empty : p.CategoryKPI.Description.ToString());
                    ColumnValues.Add(this.Columns[7], p.TypeKPI == null ? string.Empty : p.TypeKPI.Description.ToString());
                    ColumnValues.Add(this.Columns[8], p.SubTypeKPI == null ? string.Empty : p.SubTypeKPI.Description.ToString());
                    ColumnValues.Add(this.Columns[9], p.Periode.ToString());
                    ColumnValues.Add(this.Columns[10], p.Weight.ToString());

                    // ColumnValues.Add(this.Columns[12], p.ID.ToString());
                    collection.Add(idx, ColumnValues);
                }
            }

            IList<IDictionary<string, string>> aaData = collection.Values.ToList();

            var gridData = new
            {
                sEcho,
                iTotalRecords,
                iTotalDisplayRecords,
                aaData
            };
            var JSONSerializer = new JavaScriptSerializer();
            return JSONSerializer.Serialize(gridData);
        }

        private string RetrieveAssignedEmployee(int KPIID, int User)
        {
            string returnValue = string.Empty;
            int idx = 0;
            //IList<KPIAssign> KPIAssigns = new KPIAssignFacade().GetAllKPIAssignsByKPIID(KPIID);

            //if (KPIAssigns != null && KPIAssigns.Any())
            //{
            // IList<KPIAssign> UsedKPIAssigns = KPIAssigns;//.Where(x => x.Verifier.ID == User).ToList();

            //if (UsedKPIAssigns != null && UsedKPIAssigns.Any())
            //{
            //foreach (KPIAssign a in UsedKPIAssigns)
            //foreach(KPIAssign a in KPIAssigns)
            //    {
            IList<KPIAssignDetail> KPIs = new KPIAssignDetailFacade().GetEmployeeKPIAssignDetailByKPIAssignID(KPIID);
            if (KPIs.Count > 0)
            {
                foreach (KPIAssignDetail k in KPIs)
                {
                    if (idx == 0)
                    {
                        returnValue = k.Employee.FirstName + " " + k.Employee.LastName;
                    }
                    else
                    {
                        returnValue += ", " + k.Employee.FirstName + " " + k.Employee.LastName;
                    }
                    idx++;
                }
            }
            //}
            //}
            //}



            return returnValue;
        }
    }
}